package com.jlastu.dlxy_androidapp.view.main;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.transition.Slide;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.adapter.GoodsPhotoAdapter;
import com.jlastu.dlxy_androidapp.adapter.SchoolPhotoAdapter;
import com.jlastu.dlxy_androidapp.contract.AddSchoolContract;


import com.jlastu.dlxy_androidapp.entity.Goods;
import com.jlastu.dlxy_androidapp.entity.Photo;
import com.jlastu.dlxy_androidapp.entity.School;
import com.jlastu.dlxy_androidapp.entity.SchoolPhoto;

import com.jlastu.dlxy_androidapp.presenter.AddSchoolPresenter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AddSchoolActivity extends AppCompatActivity implements
        SchoolPhotoAdapter.OnPhotoListener, AddSchoolContract.IAddSchoolView, View.OnClickListener {

    private RecyclerView mPhotoRecyclerView;
    private EditText mSchoolNameEdit;
    private EditText mSchoolKnowEdit;
    private Button mSendButton;
    private List<SchoolPhoto> mPhotoList;
    private SchoolPhotoAdapter mSchoolPhotoAdapter;
    private ActivityResultLauncher mActivityResultLauncher;
    private AddSchoolContract.IAddSchoolPresenter mAddSchoolPresenter;

    private ImageButton mSchoolClassify;

    private RadioGroup mClassify;
    private RadioButton mRadioButton_1;
    private RadioButton mRadioButton_2;
    @Override
    public void onConfigurationChanged(android.content.res.Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // 检查屏幕方向是否为横屏
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.d("Orientation", "Landscape detected");

            // 创建并启动新的Activity
            Intent intent = new Intent(this, LandscapeActivity.class);
            startActivity(intent);

            // 可选：关闭当前Activity
            finish();
        }
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setEnterTransition(new Slide());
        setContentView(R.layout.activity_add_school);
        mAddSchoolPresenter = new AddSchoolPresenter(this, this);
        mPhotoList = new ArrayList<>();
        mPhotoList.add(new SchoolPhoto());
        mPhotoRecyclerView = findViewById(R.id.rv_school_photo);
        mSchoolNameEdit = findViewById(R.id.et_school_name);
        mSchoolKnowEdit = findViewById(R.id.know_ed);
        mSendButton = findViewById(R.id.school_send);
        mSendButton.setOnClickListener(this);
        mSchoolClassify = findViewById(R.id.arrow_icon);
        mSchoolClassify.setOnClickListener(this);
        mClassify = findViewById(R.id.radioGroup);
        mRadioButton_1 = findViewById(R.id.radioButtonExpert);
        mRadioButton_2 = findViewById(R.id.radioButtonBreaknew);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        mPhotoRecyclerView.setLayoutManager(gridLayoutManager);
        mSchoolPhotoAdapter = new SchoolPhotoAdapter(mPhotoList);
        mSchoolPhotoAdapter.setPhotoListener(this);
        mPhotoRecyclerView.setAdapter(mSchoolPhotoAdapter);
        onActivityResult();
        mClassify.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radioButtonExpert) {
                    // 专家咨询被选中
                    RadioButton selectedRadioButton = mClassify.findViewById(mClassify.getCheckedRadioButtonId());
                    String selectedOption = selectedRadioButton.getText().toString();
                    Log.d("TAG", "onCheckedChanged: " + mClassify.getCheckedRadioButtonId());
                    Log.d("TAG", "文本: " + selectedOption);

                } else if (checkedId == R.id.radioButtonBreaknew) {
                    // 创业项目被选中
                    RadioButton selectedRadioButton = mClassify.findViewById(mClassify.getCheckedRadioButtonId());
                    String selectedOption = selectedRadioButton.getText().toString();
                    Log.d("TAG", "onCheckedChanged: " + mClassify.getCheckedRadioButtonId());
                    Log.d("TAG", "文本: " + selectedOption);
                }
            }
        });

    }


    private void onActivityResult() {
        mActivityResultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        int resultCode = result.getResultCode();
                        if (resultCode == RESULT_OK) {
                            Intent data = result.getData();
                            Uri selectedImageUri = data.getData();
                            try {
                                // 使用图片URI进行处理，例如加载到ImageView中
                                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
                                SchoolPhoto photo = new SchoolPhoto();
                                photo.setBitmap(bitmap);
                                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                                Bitmap resized = Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth() * 0.1), (int) (bitmap.getHeight() * 0.1), true);
                                resized.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                                photo.setData(outputStream.toByteArray());
                                mSchoolPhotoAdapter.addPhoto(photo);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.school_send) {
            RadioButton selectedRadioButton = mClassify.findViewById(mClassify.getCheckedRadioButtonId());
            String selectedOption = selectedRadioButton.getText().toString();

            if (mSchoolNameEdit.getText().toString().isEmpty()) {
                Toast.makeText(this, "请填写文章名称", Toast.LENGTH_SHORT).show();
            } else if (mSchoolPhotoAdapter.getPhotoList().isEmpty()) {
                Toast.makeText(this, "请添加封面图片", Toast.LENGTH_SHORT).show();
            } else if (selectedOption.isEmpty()) {
                Toast.makeText(this, "请选择文章分类", Toast.LENGTH_SHORT).show();
            }else if (mSchoolKnowEdit.getText().toString().isEmpty()) {
                Toast.makeText(this, "请填写正文内容", Toast.LENGTH_SHORT).show();
            }  else {
                School school = new School();
                school.setName(mSchoolNameEdit.getText().toString());
                school.setKnow(mSchoolKnowEdit.getText().toString());
                school.setClassify(selectedOption);
                school.setSchoolphotoList(mSchoolPhotoAdapter.getPhotoList());
                mAddSchoolPresenter.saveSchool(school);
                Toast.makeText(this, "发布成功！", Toast.LENGTH_SHORT).show();
                onBackPressed();

            }

        }else if (id == R.id.arrow_icon) {
            if (mClassify.getVisibility() == View.GONE) {
                mClassify.setVisibility(View.VISIBLE);
            } else {
                mClassify.setVisibility(View.GONE);
            }

        }
    }

    @Override
    public void onAdd() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        mActivityResultLauncher.launch(intent);
    }

    @Override
    public void onDelete(int position) {

    }

    @Override
    public void showSchoolList(List<School> schoolList) {

    }
}
